package ssm.blog.service.impl;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.util.StringUtils;
import org.springframework.stereotype.Service;
import ssm.blog.dao.BlogMapper;
import ssm.blog.entity.Blog;
import ssm.blog.entity.BlogExample;
import ssm.blog.entity.User;
import ssm.blog.service.BlogService;
import ssm.blog.util.getImgSrc;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @program: lyon
 * @description: 博客-服务层
 * @author: Lyon
 * @create: 2018-05-30 17:10
 **/
@Service
public class BlogServiceImpl implements BlogService {
    @Resource
    private BlogMapper mapper;
    @Override
    public int add(Blog blog) {
        User currentUser=(User)SecurityUtils.getSubject().getSession().getAttribute("currentUser");
        if(blog.getSign().equals("")){
            blog.setSign(currentUser.getSign());
        }
        blog.setOwner_name(currentUser.getUsername());
        blog.setOwner_id(currentUser.getId());
        blog.setReleas_date(new Date());
        blog.setLikenum(0);
        blog.setCommentsnum(0);
        //提取图片资源.
        List<String>imgs= getImgSrc.getImgStr(blog.getContent());
        blog.setImg_url(StringUtils.join(imgs.iterator(),","));

        return mapper.insert(blog);
    }
    @Override
    public int deleById(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deles(List<Integer> ids) {
        BlogExample example=new BlogExample();
        example.createCriteria().andIdIn(ids);
        return mapper.deleteByExample(example);
    }

    @Override
    public int updateById(Blog blog) {
        if(blog.getSign().equals("")){
            User currentUser=(User)SecurityUtils.getSubject().getSession().getAttribute("currentUser");
            blog.setSign(currentUser.getSign());
        }
        blog.setReleas_date(new Date());
        //TODO 这里应该有图片的删除操作
        List<String>imgs= getImgSrc.getImgStr(blog.getContent());
        blog.setImg_url(StringUtils.join(imgs.iterator(),","));
        return mapper.updateByPrimaryKeySelective(blog);
    }
    @Override
    public Blog selectById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Blog> getNext(Integer id,Integer statu,Integer type_id) {
        BlogExample example=new BlogExample();
        example.setOrderByClause("id desc");
        switch (statu){
            case 1:
                //只选择公开的博客
                if(type_id==-1){
                    example.createCriteria().andIdLessThan(id).andStatuEqualTo(1);
                }else{
                    example.createCriteria().andIdLessThan(id).andStatuEqualTo(1).andType_idEqualTo(type_id);
                }
                break;
            case 2:
                //只选择公开的博客
                if(type_id==-1){
                    example.createCriteria().andIdLessThan(id).andStatuEqualTo(1).andStatuEqualTo(2);
                }else{
                    example.createCriteria().andIdLessThan(id).andStatuEqualTo(1).andStatuEqualTo(2).andType_idEqualTo(type_id);
                }
                break;
            default: break;
        }
        return mapper.selectByExample(example);
    }

    @Override
    public List<Blog> getPre(Integer id,Integer statu,Integer type_id) {
        BlogExample example=new BlogExample();
        example.setOrderByClause("id asc");
        switch (statu){
            case 1:
                //只选择公开的博客
                example.createCriteria().andIdGreaterThan(id).andStatuEqualTo(1).andType_idEqualTo(type_id);
                break;
            case 2:
                //只选择公开的博客
                example.createCriteria().andIdGreaterThan(id).andStatuEqualTo(1).andStatuEqualTo(2).andType_idEqualTo(type_id);
                break;
            default: break;
        }
        return mapper.selectByExample(example);
    }

    @Override
    public List<Blog> listByKeyword(String keyword) {
        BlogExample example=new BlogExample();
        example.setOrderByClause("id desc");
        example.createCriteria().andTitleLike("%"+keyword+"%");
        return mapper.selectByExample(example);
    }

    @Override
    public List<Blog> listByKeywordAndTypeId(String keyword, Integer type_id) {
        BlogExample example=new BlogExample();
        example.setOrderByClause("id desc");
        example.createCriteria().andType_idEqualTo(type_id).andTitleLike("%"+keyword+"%");
        return mapper.selectByExample(example);
    }

    @Override
    public List<Blog> listRank(Integer statu) {
        BlogExample example=new BlogExample();
        example.setOrderByClause("likenum desc");
        setStatu(example,statu);
        return mapper.selectByExample(example);
    }

    @Override
    public List<Blog> listBlogs(Integer statu,Integer type_id,String keyword,Boolean down) {
        BlogExample example=new BlogExample();
        if(down){
            example.setOrderByClause("id desc");
        }else{
            example.setOrderByClause("id asc");
        }
        if(type_id!=-1){
            switch (statu){
                case 1:
                    //只选择公开的博客
                    example.createCriteria().andStatuEqualTo(1).andType_idEqualTo(type_id).andTitleLike("%"+keyword+"%");
                    break;
                case 2:
                    //只选择公开的博客
                    example.createCriteria().andStatuEqualTo(1).andStatuEqualTo(2).andType_idEqualTo(type_id).andTitleLike("%"+keyword+"%");
                    break;
                default: break;
            }
        }else{
            switch (statu){
                case 1:
                    //只选择公开的博客
                    example.createCriteria().andStatuEqualTo(1).andTitleLike("%"+keyword+"%");
                    break;
                case 2:
                    //只选择公开的博客
                    example.createCriteria().andStatuEqualTo(1).andStatuEqualTo(2).andTitleLike("%"+keyword+"%");
                    break;
                default: break;
            }
        }
        return mapper.selectByExample(example);
    }

    @Override
    public int count() {
        BlogExample example=new BlogExample();
        return mapper.countByExample(example);
    }

    private BlogExample setStatu(BlogExample example,Integer statu){
        switch (statu){
            case 1:
                //只选择公开的博客
                example.createCriteria().andStatuEqualTo(1);
                break;
            case 2:
                //只选择公开的博客
                example.createCriteria().andStatuEqualTo(1).andStatuEqualTo(2);
                break;
            default: break;
        }
        return example;
    }
}
